import "package:ct_pa_majordomo/config/theme_vars.dart";
import "package:ct_pa_majordomo/pages/check_in_detail/api.dart";
import "package:ct_pa_majordomo/pages/widgets/form_select_field.dart";
import "package:ct_pa_majordomo/pages/widgets/menu_block.dart";
import "package:ct_pa_majordomo/utils/index.dart";
import "package:ct_pa_majordomo/widgets/bottom_sheet/index.dart";
import "package:ct_pa_majordomo/widgets/button/index.dart";
import "package:ct_pa_majordomo/widgets/normal_scaffold/index.dart";
import "package:flutter/cupertino.dart";
import "package:flutter/material.dart";
import "package:flutter_easyloading/flutter_easyloading.dart";

class RentInfo extends StatefulWidget {
  final Function changeInfo;
  final Map? rentObject;
  const RentInfo({super.key, required this.changeInfo, this.rentObject, });

  @override
  State<StatefulWidget> createState() => _RentInfoState();

}

class _RentInfoState extends State<RentInfo> {
  final TextEditingController _depositController = TextEditingController();
  Map _params = {
    "deposit": "",
    "rent": "",
    "rentCollectCycle": 1,
    "rentType": -1
  };
  bool _selecting = false;
  String _rentCollectCycleName = "月租金";
  final Map _rentTypeDict = {
    1: "每1月",
    2: "每2月",
    3: "每3月",
    4: "每4月",
    5: "每半年",
    6: "每一年",
    7: "一次性",
  };
  final List _rentTypes = [{
    "label": "每1月",
    "value": 1
  }, {
    "label": "每2月",
    "value": 2
  }, {
    "label": "每3月",
    "value": 3
  }, {
    "label": "每4月",
    "value": 4
  }, {
    "label": "每半年",
    "value": 5
  }, {
    "label": "每一年",
    "value": 6
  }, {
    "label": "一次性",
    "value": 7
  }];
  final Widget _required = const Text("*", style: TextStyle(color: ThemeVars.colorError),);

  bool _valid() {
    return _params["rent"].toString().isNotEmpty && _params["rentType"] != -1;
  }

  void _setSelecting(bool status) {
    setState(() {
      _selecting = status;
    });
  }

  void _showRentTypeMenu() {
    _setSelecting(true);
    showRoundedBottomSheet(
      builder: (context) => BottomSheetMenu(
        onCancel: () => _setSelecting(false),
        items: [
          BottomSheetMenuItem("月租金", onTap: () {
            setState(() {
              _params["rentCollectCycle"] = 1;
              _rentCollectCycleName = "月租金";
            });
            _setSelecting(false);
          }),
          BottomSheetMenuItem("日租金", onTap: () {
            setState(() {
              _params["rentCollectCycle"] = 2;
              _rentCollectCycleName = "日租金";
            });
            _setSelecting(false);
          }),
        ],
      ),
    );
  }

  Future<void> _selectRentType() async {
    Map? popped = await showRoundedBottomSheet(
      builder: (context) => BottomSheetSelect(
        items: _rentTypes,
        compare: (item) => item["value"] == _params["rentType"],
        itemAsString: (item) => item["label"],
      ),
    );
    if (popped != null) {
      setState(() {
        _params["rentType"] = popped["value"];
      });
    };
  }

  @override
  void didChangeDependencies() {
    setState(() {
      _params = (Utils.isNotEmpty(widget.rentObject) ? widget.rentObject : _params)!;
      _depositController.text = _params["deposit"].toString();
    });
    super.didChangeDependencies();
  }


  @override
  Widget build(BuildContext context) {
    return NormalScaffold(
      title: "房租信息",
      child: ListView(
        children: [
          MenuBlock(
            items: [
              MenuBlockItem(
                text: "",
                showArrow: false,
                onTap: () {
                  _showRentTypeMenu();
                },
                prefix: Row(
                  children: [
                    _required,
                    const SizedBox(width: 10,),
                    Text(_rentCollectCycleName),
                    const SizedBox(width: 2,),
                    Icon(
                      _selecting ? Icons.arrow_drop_up_sharp : Icons.arrow_drop_down_sharp,
                      color: ThemeVars.colorTextTer,
                    )
                  ],
                ),
                suffix: TextFormField(
                  textAlign: TextAlign.right,
                  decoration: const InputDecoration(
                      hintText: "请输入",
                      hintStyle: TextStyle(color: ThemeVars.colorTextTer),
                      isDense: true
                  ),
                  initialValue: _params["rent"].toString(),
                  onChanged: (value) => setState(() {
                    _params["rent"] = value;
                    _params["deposit"] = value;
                    _depositController.text = value;
                  }),
                ),
              ),
              MenuBlockItem(
                text: "押金",
                showArrow: false,
                suffix: TextFormField(
                  controller: _depositController,
                  textAlign: TextAlign.right,
                  decoration: const InputDecoration(
                      hintText: "无押金",
                      hintStyle: TextStyle(color: ThemeVars.colorTextTer),
                      isDense: true
                  ),
                  // initialValue: _params["deposit"],
                  onChanged: (value) => setState(() {
                    _params["deposit"] = value;
                  }),
                ),
              ),
              MenuBlockItem(
                  text: "付款方式",
                  prefix: _required,
                  suffix: Text(_params["rentType"] != -1 ?  _rentTypeDict[_params["rentType"]] : "请选择"),
                  onTap: () {
                    _selectRentType();
                  }
              ),
            ],
          ),
          Padding(
            padding: EdgeInsets.symmetric(horizontal: ThemeVars.paddingPage, vertical: 30),
            child: Button(
              onPressed: _valid() ? () async {
                widget.changeInfo(_params);
                if (context.mounted) {
                  Navigator.pop(context);
                }
              } : null,
              child: const Text("确定"),
            ),
          ),
        ],
      ),
    );
  }

}